package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.ashGate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetStoneMateriallevelRecordVo;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 料位查询业务层
 * @author xiaoxu
 * @date 2023/1/13 15:00
 * @version 1.0
 * @since 1.0
 */
@Component
public class MaterialLevelQueryBu {
    @Autowired
    private ICAshGateVideoBindService icAshGateVideoBindService;
    @Autowired
    private ITAreaService itAreaService;
    @Autowired
    private ICStoneGateVideoBindService icStoneGateVideoBindService;
    @Autowired
    private ITPiStoneService itPiStoneService;
    @Autowired
    private ITStoneMaterialLevelService itStoneMaterialLevelService;
//    @Autowired
//    private
    public SzResult getMateriallevel(String plantId) {
        QueryWrapper<CAshGateVideoBind> cAshGateVideoBindQueryWrapper = new QueryWrapper<>();
// AREA_ID不为空
        cAshGateVideoBindQueryWrapper.isNotNull("AREA_ID");
//        ASHGATE_DELETED为0
        cAshGateVideoBindQueryWrapper.eq("ASHGATE_DELETED",0);
        cAshGateVideoBindQueryWrapper.eq("ASHGATE_TYPE", 2);
//        DATA_FROM为plantId
        cAshGateVideoBindQueryWrapper.eq("DATA_FROM",plantId);
        cAshGateVideoBindQueryWrapper.orderByAsc("ASHGATE_ID");
//        拿到所有数据，getlist
        List<CAshGateVideoBind> cAshGateVideoBindlist = icAshGateVideoBindService.list(cAshGateVideoBindQueryWrapper);
        QueryWrapper<TArea> tAreaQueryWrapper = new QueryWrapper<>();
//        拿到所有数据，getlist
        tAreaQueryWrapper.eq("DEL_FLAG",0);
        tAreaQueryWrapper.eq("DATA_FROM",plantId);

        List<TArea> arealist = itAreaService.list(tAreaQueryWrapper);
        Map<Object, Object> map = new HashMap<>();
//        将cAshGateVideoBindlist里有AREA_ID按照arealist的id进行分组存入map
        for (TArea tArea : arealist) {
            List<CAshGateVideoBind> cAshGateVideoBindList = new ArrayList<>();
            for (CAshGateVideoBind cAshGateVideoBind : cAshGateVideoBindlist) {
                if (cAshGateVideoBind.getAreaId().equals(tArea.getId())) {
                    cAshGateVideoBindList.add(cAshGateVideoBind);
                }
            }
            map.put(tArea.getAreaName(), cAshGateVideoBindList);
        }
        SzResult szResult = new SzResult();
        szResult.setCode(200);
        szResult.setMsg("查询成功");
        szResult.setResult(map);
        return szResult;
    }
//石粉仓料位
    public SzResult getStoneMateriallevel(String plantId) {
//        QueryWrapper<CStoneGateVideoBind> cAshGateVideoBindQueryWrapper = new QueryWrapper<>();
//// AREA_ID不为空
//        cAshGateVideoBindQueryWrapper.isNotNull("AREA_ID");
////        ASHGATE_DELETED为0
//        cAshGateVideoBindQueryWrapper.eq("ASHGATE_DELETED",0);
//        cAshGateVideoBindQueryWrapper.eq("ASHGATE_TYPE", 2);
////        DATA_FROM为plantId
//        cAshGateVideoBindQueryWrapper.eq("DATA_FROM",plantId);
////        拿到所有数据，getlist
//        List<CStoneGateVideoBind> cAshGateVideoBindlist = icStoneGateVideoBindService.list(cAshGateVideoBindQueryWrapper);
        QueryWrapper<CAshGateVideoBind> OperatorUpdateWrapper = new QueryWrapper<>();
        OperatorUpdateWrapper.eq("DATA_FROM", plantId);
        OperatorUpdateWrapper.eq("ASHGATE_DELETED",0);
        OperatorUpdateWrapper.like("ASHGATE_NAME", "粉仓");
        OperatorUpdateWrapper.eq("ASHGATE_TYPE",2);
        OperatorUpdateWrapper.orderByAsc("ASHGATE_ID");
        List<CAshGateVideoBind> AshGateMan = icAshGateVideoBindService.list(OperatorUpdateWrapper);
        SzResult szResult = new SzResult();
        szResult.setCode(200);
        szResult.setMsg("查询成功");
        szResult.setResult(AshGateMan);
        return szResult;
    }
    //获取石粉料位的更新记录getStoneMateriallevelRecord
//    这个表我没绑定datafrom，但是关联的stone表我绑定了datafrom所以当查询全部时直接查所有的属于这个厂的stoneId
    public SzResult getStoneMateriallevelRecord(Integer page1,Integer pageSize,String startTime,String endTime,String stoneId,String plantId) {
        try {
            Page<TStoneMaterialLevel> page = new Page<>(page1, pageSize);
            QueryWrapper<TStoneMaterialLevel> tStoneMaterialLevelQueryWrapper = new QueryWrapper<>();
//        tStoneMaterialLevelQueryWrapper.eq("DATA_FROM", plantId);
//     UPDATE_TIME
            tStoneMaterialLevelQueryWrapper.between(startTime != null & endTime != null, "UPDATE_TIME", startTime, endTime);
            if (stoneId != null) {
                tStoneMaterialLevelQueryWrapper.like("STONE_ID", stoneId);
            } else {
                QueryWrapper<TPiStone> tPiStoneQueryWrapper = new QueryWrapper<>();
                tPiStoneQueryWrapper.eq("DATA_FROM", plantId);
                List<TPiStone> tPiStones = itPiStoneService.list(tPiStoneQueryWrapper);
                List<String> stoneIds = new ArrayList<>();
                for (TPiStone tPiStone : tPiStones) {
                    stoneIds.add(tPiStone.getId());
                }
                if (stoneIds.size() > 0) {
                    tStoneMaterialLevelQueryWrapper.in("STONE_ID", stoneIds);
                }else {
                    return SzResult.Error("无粉仓料位数据");
                }
            }
            tStoneMaterialLevelQueryWrapper.orderByDesc("UPDATE_TIME");
            IPage<TStoneMaterialLevel> iPage = itStoneMaterialLevelService.page(page, tStoneMaterialLevelQueryWrapper);
            List<GetStoneMateriallevelRecordVo> getStoneMateriallevelRecordVos = new ArrayList<>();
            for (int i = 0; i < iPage.getRecords().size(); i++) {
                GetStoneMateriallevelRecordVo getStoneMateriallevelRecordVo = new GetStoneMateriallevelRecordVo();
                getStoneMateriallevelRecordVo.setStoneId(iPage.getRecords().get(i).getStoneId());
                getStoneMateriallevelRecordVo.setPiPoint(iPage.getRecords().get(i).getPiPoint());
                getStoneMateriallevelRecordVo.setId(iPage.getRecords().get(i).getId());
                getStoneMateriallevelRecordVo.setCurrentHeight(iPage.getRecords().get(i).getCurrentHeight());
                getStoneMateriallevelRecordVo.setUpdateTime(iPage.getRecords().get(i).getUpdateTime());
                //todo根据高度通过公示计算当前重量
                getStoneMateriallevelRecordVo.setCurrentWeight(BigDecimal.valueOf(0));
                QueryWrapper<TPiStone> tPiStoneQueryWrapper = new QueryWrapper<>();
                tPiStoneQueryWrapper.eq("ID", getStoneMateriallevelRecordVo.getStoneId());
                TPiStone tPiStone = itPiStoneService.getOne(tPiStoneQueryWrapper);
                getStoneMateriallevelRecordVo.setStoneName(tPiStone.getStoneName());
                getStoneMateriallevelRecordVos.add(getStoneMateriallevelRecordVo);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("total", getStoneMateriallevelRecordVos.size());
            map.put("list", getStoneMateriallevelRecordVos);
            return SzResult.success(map);
        }catch (Exception e){
            return SzResult.Error("未查到料位数据");
        }
    }
    //    石粉t_pi_stone表的增删改查
    public SzResult getPiStones(Integer pageNum,Integer pageSize,String stoneName,String plantId) {
        Page<TPiStone> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TPiStone> tPiStoneQueryWrapper = new QueryWrapper<>();
        tPiStoneQueryWrapper.eq("DATA_FROM", plantId);
        tPiStoneQueryWrapper.like(stoneName != null, "STONE_NAME", stoneName);
        IPage<TPiStone> iPage = itPiStoneService.page(page, tPiStoneQueryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("list", iPage.getRecords());
        return SzResult.success(map);
    }
    public SzResult getAllPiStones(String stoneName,String plantId) {
        QueryWrapper<TPiStone> tPiStoneQueryWrapper = new QueryWrapper<>();
        tPiStoneQueryWrapper.eq("DATA_FROM", plantId);
        tPiStoneQueryWrapper.like(stoneName != null, "STONE_NAME", stoneName);
        List<TPiStone> tPiStones = itPiStoneService.list(tPiStoneQueryWrapper);
        return SzResult.success(tPiStones);
    }
}
